﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;

using PGMHelper;

namespace HystereticBasic
{
    public partial class DuctilityForm : Form
    {
        /// <summary>
        /// 数据点数据
        /// </summary>
        public DuctilityObjSet ducObjSet { set; get; }

        /// <summary>
        /// 获得数据类型
        /// </summary>
        private DuctilityMethod ductilityType
        {
            get
            {
                //能量
                if (ParkRadioButton.Checked) return DuctilityMethod.Park;
                //粘滞系数
                else if (EnergyRadioButton.Checked)
                    return DuctilityMethod.Energy;
                //刚度退化系数
                else return DuctilityMethod.General;
            }
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="datalist"></param>
        public DuctilityForm(DuctilityObjSet ducObjSet)
        {
            InitializeComponent();
            //获得数据
            this.ducObjSet = ducObjSet;
            //导入文件列表数据
            this.FileNameListBox.SetDataSource(this.ducObjSet.DucObjList, "Name", "Name");
            //数据
            this.GridView.AddDatas(this.ducObjSet.GetDuctilityData());
            this.initial();
            //控件关闭
            this.DirectPathGroupbox.Enabled = false;
            this.ReadButton.Enabled = false;
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        public DuctilityForm()
        {
            InitializeComponent();
            this.ducObjSet = new DuctilityObjSet();
            this.initial();
        }

        /// <summary>
        /// 初始化
        /// </summary>
        private void initial()
        {
            //控件外观
            this.GridView.SetApperance(DataGridViewSelectionMode.FullRowSelect);
            this.tabControl1.SetApperance();
            HystereticHelper.SetChartApperance(this.DuctilityChart);
            //设定坐标轴最大值
            this.DuctilityChart.SetAxisBroundary(ChartAxisType.AxisX, 0);
            this.DuctilityChart.SetAxisBroundary(ChartAxisType.AxisY, 0);
            //初始化参数
            this.UltimateStrengthCoTextBox.Text = GlobalParam.UltimateForceFactor.ToString("f2");
            this.ParkCoTextBox.Text = GlobalParam.ParkFactor.ToString("f2");
        }

        /// <summary>
        /// 显示指定数据
        /// </summary>
        /// <param name="data"></param>
        private void GetDetailData(DuctilityObj obj)
        {
            //峰值承载力与对应位移
            obj.PeakUltimateTextbox(new List<TextBox>
            {
                this.PeakDispTextBox, this.PeakCapacityTextBox,
                this.UltimateDispTextBox, this.UltimateCapacityTextBox
            });
            //通用弯矩屈服法对应的数据
            obj.DuctilityToTextbox(new List<TextBox>
            {
                this.Yield_EYD_TextBox, this.Yield_EYF_TextBox, this.Yield_D_TextBox
            }, DuctilityMethod.General);
            //能量法对应的数据
            obj.DuctilityToTextbox(new List<TextBox>
            {
                this.Energy_EYD_TextBox, this.Energy_EYF_TextBox, this.Energy_D_TextBox
            }, DuctilityMethod.Energy);
            //Park法对应的数据
            obj.DuctilityToTextbox(new List<TextBox>
            {
                this.Park_EYD_TextBox, this.Park_EYF_TextBox, this.Park_D_TextBox
            }, DuctilityMethod.Park);
            //平均值对应的数据
            obj.DuctilityToTextbox(new List<TextBox>
            {
                this.Mean_EYD_TextBox, this.Mean_EYF_TextBox, this.Mean_D_TextBox
            }, DuctilityMethod.AVG);
        }

        /// <summary>
        /// 更新图表和计算结果
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void RefreshButton_Click(object sender, EventArgs e)
        {
            //更新参数
            float UltimateStrengthCo = float.Parse(this.UltimateStrengthCoTextBox.Text);
            float ParkCo = float.Parse(this.ParkCoTextBox.Text);
            //计算
            this.ducObjSet.SetDuctilityFactor(UltimateStrengthCo, ParkCo);
            //数据
            this.GridView.AddDatas(this.ducObjSet.GetDuctilityData());
            //更新
            this.RefreshForm();
        }

        /// <summary>
        /// 导出数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OutputButton_Click(object sender, EventArgs e)
        {
            //导出Excel
            NPOI.Helper.NPOIExcelHelper.SaveDataToExcel(this.ducObjSet.GetDuctilityData(), true);
        }

        /// <summary>
        /// 双击显示
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FileNameListBox_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            this.RefreshForm();
        }

        /// <summary>
        /// 更新
        /// </summary>
        private void RefreshForm()
        {
            //是否存在对象
            if (this.ducObjSet.DucObjList.Count == 0) return;
            //获得选择对象
            var data = (DuctilityObj)this.FileNameListBox.GetSelectObject();
            //绘制曲线
            data.ToChart(this.DuctilityChart, this.ductilityType);
            //更新详细数据
            this.GetDetailData(data);
        }

        /// <summary>
        /// 读取构件信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ReadButton_Click(object sender, EventArgs e)
        {
            //更新参数
            float UltimateStrengthCo = float.Parse(this.UltimateStrengthCoTextBox.Text);
            float ParkCo = float.Parse(this.ParkCoTextBox.Text);
            //所有文件路径
            var filePathList = HystereticHelper.GetFileList(DirectPathTextbox.Text);
            //路径为空
            if (filePathList.Count == 0) return;
            //获得数据
            this.ducObjSet = new DuctilityObjSet(filePathList, 0, UltimateStrengthCo, ParkCo);
            //导入文件列表数据
            this.FileNameListBox.SetDataSource(this.ducObjSet.DucObjList, "Name", "Name");
            //数据
            this.GridView.AddDatas(this.ducObjSet.GetDuctilityData());
        }

        /// <summary>
        /// 选项切换
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void RadioButtonClick(object sender, EventArgs e)
        {
            this.RefreshForm();
        }
    }
}
